package com.letu.mq.event;

import com.alibaba.fastjson.JSONObject;
import com.letu.cache.repository.CachePlusOps;
import com.letu.core.point.BusinBasePoint;
import com.letu.core.point.LogBasePoint;
import com.letu.core.point.OrderBusinBasePoint;
import com.letu.core.rocketmq.MqCacheKeyBuilder;
import com.letu.core.utils.StringUtils;
import com.letu.mq.model.MySimpleMessage;
import com.letu.mq.util.MessageExtUtil;
import com.letu.mq.util.MessageKeyUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.Validators;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.*;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageAccessor;
import org.apache.rocketmq.common.message.MessageConst;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 发送事件
 */
@Slf4j
@Component
public class ProducerEvent {
//    @Autowired
//    private DefaultMQProducer defaultProducer;
    @Autowired
    private CachePlusOps cachePlusOps;

//    @Autowired
//    private RocketMQTemplate rocketMQTemplate;

    @Autowired
    private MessageExtUtil messageExtUtil;
    /**
     * 单向发送
     *
     * @param mySimpleMessage
     */
    public void oneWaySend(MySimpleMessage mySimpleMessage) {
        messageExtUtil.assembleHeader(mySimpleMessage);
        //唯一业务key 用于幂等
        cachePlusOps.set(new MqCacheKeyBuilder().hashKey(mySimpleMessage.getHeaders().get("key").toString()), 1);
//        rocketMQTemplate.sendOneWay(mySimpleMessage.getTopic(), mySimpleMessage);
    }


    /**
     * 同步发送
     *
     * @param mySimpleMessage
     */
    public void sycnSend(MySimpleMessage mySimpleMessage) {
        messageExtUtil.assembleHeader(mySimpleMessage);
        //唯一业务key 用于幂等
        cachePlusOps.set(new MqCacheKeyBuilder().hashKey(mySimpleMessage.getHeaders().get("key").toString()), 1);
//        rocketMQTemplate.convertAndSend(mySimpleMessage.getTopic(), mySimpleMessage);
    }

    /**
     * 异步发送
     *
     * @param mySimpleMessage
     */
    public void asycnSend(MySimpleMessage mySimpleMessage) {
        messageExtUtil.assembleHeader(mySimpleMessage);
        //唯一业务key 用于幂等
//        cachePlusOps.set(new MqCacheKeyBuilder().hashKey(mySimpleMessage.getHeaders().get("key").toString()), 1);
////        rocketMQTemplate.convertAndSend(mySimpleMessage.getTopic(), mySimpleMessage);
////        rocketMQTemplate.asyncSend(mySimpleMessage.getTopic(), mySimpleMessage, new SendCallback() {
//            @Override
//            public void onSuccess(SendResult sendResult) {
//                System.out.printf("send OK %s %n",
//                        sendResult.getMsgId());
//            }
//
//            @Override
//            public void onException(Throwable e) {
//                System.out.printf("send Exception %s %n", e);
//                e.printStackTrace();
//            }
//        });
    }

    /**
     * 顺序发送
     *
     * @param mySimpleMessage
     */
    public void orderSend(MySimpleMessage mySimpleMessage) {
        OrderBusinBasePoint orderBusinBasePoint = (OrderBusinBasePoint)mySimpleMessage.getPayload();
        messageExtUtil.assembleHeader(mySimpleMessage);
        //唯一业务key 用于幂等
        cachePlusOps.set(new MqCacheKeyBuilder().hashKey(mySimpleMessage.getHeaders().get("key").toString()), 1);
//        rocketMQTemplate.syncSendOrderly(mySimpleMessage.getTopic(), mySimpleMessage,orderBusinBasePoint.getOrderPointOrderId());
    }

    /**
     * 订单事务发送
     *
     * @param message
     */
    public void orderTransactionSend(Message message) {
//        try {
//            String key = MessageKeyUtil.assembleKey();
//            //唯一业务key 用于幂等
//            message.setKeys(key);
//            //唯一业务key 用于幂等
//            cachePlusOps.set(new MqCacheKeyBuilder().hashKey(key),1);
//            //发送事务消息
//            transactionProducer.sendMessageInTransaction(message,key);
//        } catch (MQClientException e) {
//            e.printStackTrace();
//        }
    }
}
